import 'package:chop/Model/address_entity.dart';
import 'package:chop/common/DioUtils.dart';
import 'package:chop/common/SPUtils.dart';
import 'package:chop/common/configs.dart';
import 'package:chop/view/home/do_job/Delivery.dart';
import 'package:chop/view/sendyou/New_address.dart';
import 'package:chop/widgets/TextSizeWidget.dart';
import 'package:chop/widgets/appBar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/ball_pulse_footer.dart';
import 'package:flutter_easyrefresh/ball_pulse_header.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:loading_animations/loading_animations.dart';

// ignore: camel_case_types
class sendAddressPage extends StatefulWidget {
  final bool isSend;

  const sendAddressPage({Key key, this.isSend}) : super(key: key);
  @override
  _sendAddressPageState createState() => _sendAddressPageState();
}

//发货地址列表
// ignore: camel_case_types
class _sendAddressPageState extends State<sendAddressPage> {

  //1 发货地址 or  0送达地址
  String isSend;

  // 发货地址列表
  AdrEntity data;
  bool isNetworkNotBad = false;
  Color bgColor = Color.fromRGBO(212, 212, 212, 1);
  // EasyRefresh控制器
  EasyRefreshController easyRefreshController = EasyRefreshController();
  // 获取当前用户id
  Future<String> getUID() async {
    return await SPUtils.get("UID");
  }

  int pageNum = 1;
  //获取当前用户发货地址列表
  void getAdrs() async {
    String uid =  await SPUtils.get("UID");
    await DioUtils.selectByUID(uid, isSend).then((AdrEntity adrEntity) {
      setState(() {
        isNetworkNotBad = true;
        data = adrEntity;
      });
    });
  }

  @override
  void initState() {
    this.isSend = widget.isSend ? "1" : "0";

    getAdrs();
    // TODO: implement initState
    super.initState();
  }

  Widget getBody() {
    if (isNetworkNotBad) {
      if(data.code == 500){
        return Center (child: FixedSizeText("连接服务器失败",style: TextStyle(color: Colors.black45,fontSize: 20),),);
      }else if(data.data.length == 0){
        return Center (child: FixedSizeText("暂无地址",style: TextStyle(color: Colors.black45,fontSize: 20),),);
  }else{
       print("isOK");
       return ListView.builder(
         itemBuilder: (BuildContext buildContext, int i) {
           return _buildBody(data.data[i]);
         },
         itemCount: data.data.length,
       );
      }
    } else {
      print("正在加载");
      return Center(child:LoadingJumpingLine.square());
    }
  }

  Widget _buildBody(AdrEntityData row) {
    return GestureDetector(
      child: Container(
          width: double.infinity,
          color: Colors.white,
          child: Column(children: <Widget>[
            Container(
              width: double.infinity,
              padding: EdgeInsets.all(10),
              child: Row(children: <Widget>[
                Expanded(
                    flex: 6,
                    child: Column(
                      children: <Widget>[
                        Container(
                          width: double.infinity,
                          padding: EdgeInsets.only(left: 10, bottom: 2),
                          child: FixedSizeText(
                            "${row.address}",
                            textAlign: TextAlign.left,
                            style: TextStyle(fontSize: 18),
                          ),
                        ),
                        Container(
                          width: double.infinity,
                          padding: EdgeInsets.only(left: 10, bottom: 2),
                          child: FixedSizeText(
                            "${row.remark ?? ''}",
                            textAlign: TextAlign.left,
                            style: TextStyle(),
                          ),
                        ),
                        Container(
                          width: double.infinity,
                          padding: EdgeInsets.only(left: 10, bottom: 2),
                          child: FixedSizeText(
                            "${row.name}   ${row.phone}",
                            textAlign: TextAlign.left,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(color: Colors.black38),
                          ),
                        ),
                      ],
                    )),
                Expanded(
                  flex: 4,
                  child: Icon(Icons.arrow_downward),
                ),
              ]),
            ),
            Container(
              height: 1,
              color: bgColor,
            )
          ])),
      onTap: () {
        Get.back(result: row);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BaseViewBar(
        childView: BaseTitleBar("地址列表", rightText: "新增地址", rightClick: () async {
          var result = await Get.to(New_address(sOrT: isSend));
          if(result != null && result == 1){
            easyRefreshController.callRefresh();
          }
        }),
        preferredSize: Size.fromHeight(50.0),
      ),
      body: EasyRefresh(
        controller: easyRefreshController,
        header: BallPulseHeader(),
//        footer: BallPulseFooter(),
        child: getBody(),
        onRefresh: () async {
          setState(() {
            isNetworkNotBad = false;
          });
          getAdrs();
//          await getOrderItem(context,isOk: isOk);
        },
      ),
      backgroundColor: bgColor,
    );
  }
}
